package com.university.evaluation.controller;

import com.university.evaluation.entity.EvaluationSystem;
import com.university.evaluation.service.EvaluationSystemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/evaluation-system")
@Tag(name = "测评体系管理", description = "提供测评体系相关的API接口")
public class EvaluationSystemController {

    @Autowired
    private EvaluationSystemService evaluationSystemService;

    @Operation(
        summary = "获取当前生效的测评体系",
        description = "查询当前正在使用的测评体系，返回体系的详细信息包括名称、学年、学期等"
    )
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取成功",
                    content = @Content(schema = @Schema(implementation = EvaluationSystem.class))),
        @ApiResponse(responseCode = "404", description = "当前没有生效的测评体系")
    })
    @GetMapping("/current")
    public ResponseEntity<?> getCurrentSystem() {
        try {
            log.info("获取当前生效的测评体系请求");
            EvaluationSystem system = evaluationSystemService.getCurrentSystem();
            if (system != null) {
                log.info("获取当前生效的测评体系成功，体系ID: {}, 体系名称: {}", system.getId(), system.getSystemName());
                return ResponseEntity.ok(system);
            }
            log.warn("当前没有生效的测评体系");
            return ResponseEntity.status(404).body("当前没有生效的测评体系");
        } catch (Exception e) {
            log.error("获取当前生效的测评体系时发生异常: {}", e.getMessage(), e);
            return ResponseEntity.status(500).body("获取测评体系失败: " + e.getMessage());
        }
    }

    @Operation(
        summary = "获取测评体系列表",
        description = "查询所有测评体系，支持按条件筛选"
    )
    @GetMapping
    public ResponseEntity<?> getSystemList() {
        try {
            log.info("获取测评体系列表请求");
            List<EvaluationSystem> systems = evaluationSystemService.list();
            log.info("获取测评体系列表成功，共找到 {} 个测评体系", systems.size());
            return ResponseEntity.ok(systems);
        } catch (Exception e) {
            log.error("获取测评体系列表时发生异常: {}", e.getMessage(), e);
            return ResponseEntity.status(500).body("获取测评体系列表失败: " + e.getMessage());
        }
    }

    @Operation(
        summary = "启用测评体系", 
        description = "将指定的测评体系设置为当前生效的体系"
    )
    @PutMapping("/{systemId}/enable")
    public ResponseEntity<?> enableSystem(
            @Parameter(name = "systemId", description = "测评体系ID", required = true) 
            @PathVariable Long systemId) {
        try {
            log.info("启用测评体系请求，体系ID: {}", systemId);
            boolean result = evaluationSystemService.enableSystem(systemId);
            if (result) {
                log.info("测评体系启用成功，体系ID: {}", systemId);
                return ResponseEntity.ok("测评体系启用成功");
            }
            log.warn("测评体系启用失败，体系ID: {}", systemId);
            return ResponseEntity.badRequest().body("测评体系启用失败");
        } catch (Exception e) {
            log.error("启用测评体系时发生异常: {}", e.getMessage(), e);
            return ResponseEntity.status(500).body("启用测评体系失败: " + e.getMessage());
        }
    }

    @Operation(
        summary = "关闭测评体系", 
        description = "关闭指定的测评体系"
    )
    @PutMapping("/{systemId}/close")
    public ResponseEntity<?> closeSystem(
            @Parameter(name = "systemId", description = "测评体系ID", required = true) 
            @PathVariable Long systemId) {
        try {
            log.info("关闭测评体系请求，体系ID: {}", systemId);
            boolean result = evaluationSystemService.closeSystem(systemId);
            if (result) {
                log.info("测评体系关闭成功，体系ID: {}", systemId);
                return ResponseEntity.ok("测评体系关闭成功");
            }
            log.warn("测评体系关闭失败，体系ID: {}", systemId);
            return ResponseEntity.badRequest().body("测评体系关闭失败");
        } catch (Exception e) {
            log.error("关闭测评体系时发生异常: {}", e.getMessage(), e);
            return ResponseEntity.status(500).body("关闭测评体系失败: " + e.getMessage());
        }
    }

    @Operation(
        summary = "添加测评体系", 
        description = "创建新的测评体系"
    )
    @PostMapping
    public ResponseEntity<?> addSystem(
            @Parameter(name = "system", description = "测评体系信息", required = true) 
            @RequestBody EvaluationSystem system) {
        try {
            log.info("添加测评体系请求，体系名称: {}", system.getSystemName());
            boolean saved = evaluationSystemService.save(system);
            if (saved) {
                log.info("添加测评体系成功，体系ID: {}", system.getId());
                return ResponseEntity.ok(system);
            }
            log.warn("添加测评体系失败，体系名称: {}", system.getSystemName());
            return ResponseEntity.badRequest().body("添加测评体系失败");
        } catch (Exception e) {
            log.error("添加测评体系时发生异常: {}", e.getMessage(), e);
            return ResponseEntity.status(500).body("添加测评体系失败: " + e.getMessage());
        }
    }

    @Operation(
        summary = "更新测评体系", 
        description = "更新指定ID的测评体系信息"
    )
    @PutMapping("/{systemId}")
    public ResponseEntity<?> updateSystem(
            @Parameter(name = "systemId", description = "测评体系ID", required = true) 
            @PathVariable Long systemId,
            @Parameter(name = "system", description = "更新后的测评体系信息", required = true) 
            @RequestBody EvaluationSystem system) {
        try {
            log.info("更新测评体系请求，体系ID: {}", systemId);
            system.setId(systemId);
            boolean updated = evaluationSystemService.updateById(system);
            if (updated) {
                log.info("更新测评体系成功，体系ID: {}", systemId);
                return ResponseEntity.ok(system);
            }
            log.warn("更新测评体系失败，体系ID: {}", systemId);
            return ResponseEntity.badRequest().body("更新测评体系失败");
        } catch (Exception e) {
            log.error("更新测评体系时发生异常: {}", e.getMessage(), e);
            return ResponseEntity.status(500).body("更新测评体系失败: " + e.getMessage());
        }
    }
}